
var Device = {};

Device.isiPhone = function() {
   return navigator.userAgent.indexOf('iPhone') != -1;
}
Device.isiPod = function() {
   return navigator.userAgent.indexOf('iPod') != -1;
}
Device.isiPad = function() {
   return navigator.userAgent.indexOf('iPad') != -1;
}
Device.isAndroid = function() {
   return navigator.userAgent.indexOf('Android') != -1;
}
Device.isiOS = function() {
   return Device.isiPhone() || Device.isiPod() || Device.isiPad() || Device.isAndroid();
}

var graphics = {x: 768, y: 1024};
var retina = false;
var scale = 1;

var playerCount = 10;
var wordsCount = 6;

var teams = [
	{players: ["Мария Николаевна", "Игорь Петрович"], index: 1}, 
	{players: ["Игрок 3", "player 4"], index: 2},
	{players: ["Игрок 5", "player 2w345636"], index: 3},
	{players: ["Игрок 7", "player 23"], index: 4},
	{players: ["Игрок 9", "player 29"], index: 5},
	];
var words = ["1", "2"];
var curPlayer;
var curTeam;
var curWord;

function shuffle( array ) {
    for(var j, x, i = array.length; i; j = parseInt(Math.random() * i), x = array[--i], array[i] = array[j], array[j] = x);
    return true;
}

function openGameRule(){
	$("#rule").css("display", "block");
}

function isSupportTouch(){
	return "ontouchend" in document;
}

$(document).ready(function() {
	var file = location.pathname.substring(location.pathname.lastIndexOf('/') + 1);
	if(file == 'index2.html') { 
		graphics = {x: 1536, y: 2048};
		retina = true;
		scale = 2;
	}
	
	var scaleX = window.innerWidth/graphics.x;
	var scaleY = window.innerHeight/graphics.y;
	var s = Math.min(scaleY, scaleX);
	$(".scale").css("-webkit-transform", "scale("+s+")");
	$(".scaleX").css("-webkit-transform", "scale("+scaleX+", "+scaleY+")");
	//$("html").css("height", 0);
	//$("html").css("width", 0);
	
	$(".center4").css("top", (window.innerHeight-graphics.y*s)/4);
	$(".center").css("top", (window.innerHeight-graphics.y*s)/2);
	$(".bottom").css("top", window.innerHeight-graphics.y*s-1);
	$(".block").css("height", window.innerHeight);
	$(".block").css("width", window.innerWidth);
	$("#gameDialogBox").css("height", 0);
	
	//$(".page").css("top", (window.innerHeight/scale-1024)/2);
	//$(".page").css("top", (window.innerHeight-1024*scale)/2);
	
	$(".button").bind(isSupportTouch() ? "touchstart" : "mousedown", function(){
		$(this).addClass("active");
	});

	$("html").bind(isSupportTouch() ? "touchend" : "mouseup", function(){
		$(".button.active").removeClass("active");
		$(".selectbox").css("display", "none");
	});

	$(".radio").live("click", function(){
		var id = $(this).attr("tag");
		if(!$(this).hasClass("radio-active")){
			$(".radio[tag='"+id+"']").removeClass("radio-active");	
			$(this).addClass("radio-active");
		}
	});
	
	$("#main_btn_start").click(function(){
		//openGamePage(true);
		openEnterPlayersPage();
	});
	
	$("#main_btn_settings").click(function(){
		openSettingsPage();
	});
	
	$("#main_btn_rule").click(function(){
		openRulePage(true);
		//openGameRule();
		//playSound(buttonSound);
		//localStorage.clear();  
	});
	
	$("#rule").click(function(){
		$(this).css("display", "none");
	});	
	
	$("#rule_ok").click(function(){
		closeRulePage();
	});	
	
});

var isMainRule = false;

function openRulePage(main){
	isMainRule = main;
	openStarsPage("rule_page");
}

function closeRulePage(){
	if(isMainRule){
		openStarsPage("main_page");
	} else{
		openGamePage(false);
	}
}

function androidInit(){
	$("html").css("-webkit-user-select", "none");
	$("html").css("-webkit-touch-callout", "none");
	$("html").css("-webkit-tap-highlight-color", "rgba(0,0,0,0)");
}
//
//document.ontouchmove = function(event){
//	event.preventDefault();
//}
